const http = require('http')
const fs = require('fs')
const path = require('path')
const url = require('url')
const server = http.createServer((req,res)=>{
    let pathname = url.parse(req.url).pathname;
    let filename = pathname.substr(1);
    console.log(filename);
    const fs = require('fs');
const path = require('path');

function fn(filePath, index) {
  let arr = fs.readdirSync(filePath);
  for (let item of arr) {
    let lastStrIs = filePath.endsWith('/');
    let yb = lastStrIs ? filePath + item : `${filePath}/${item}`;
    let flag = fs.statSync(yb).isDirectory();
    if (!flag) {
      if (item === index || index=="") {
        return yb;
      }
    } else {
      let result = fn(yb, index);
      if (result) {
        return result;
      }
    }
  }
  return "";  // 如果当前目录没找到，就返回空字符串
}
let result = fn("../", filename);
    if(result ===""){
        res.end("not found")
    }else{
        let w = fs.readFileSync('../作业/sucai/index.html')
        res.end(w)
    }
})
server.listen(3000,()=>{
    console.log("服务器启动成功");
})



